﻿using System;
using System.IO;

namespace FileDeleteConsoleApp
{
    public static class FileDeleteTools
    {

        /// <summary>
        /// 删除指定日期格式名称的文件夹
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="dateString"></param>
        /// <returns></returns>
        public static bool FileDelete(string srcPath, string dateString = null)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        if (i.Name == dateString)
                        {
                            DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                            subdir.Delete(true);          //删除子目录和文件
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

    }
}
